package com.example.taximeter.service.impl;

import com.example.taximeter.bean.*;
import com.example.taximeter.mapper.*;
import com.example.taximeter.service.TaximeterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计价器管理
 */
@Service
public class TaximeterServiceImpl implements TaximeterService {

    private TaximeterMapper taximeterMapper;
    private FactMapper factMapper;
    private PriceMapper priceMapper;
    private TaximeterTypeMapper taximeterTypeMapper;
    private CompanyMapper companyMapper;

    @Autowired
    public TaximeterServiceImpl(TaximeterMapper taximeterMapper, FactMapper factMapper, PriceMapper priceMapper, TaximeterTypeMapper taximeterTypeMapper, CompanyMapper companyMapper){
        this.taximeterMapper = taximeterMapper;
        this.factMapper = factMapper;
        this.priceMapper = priceMapper;
        this.taximeterTypeMapper = taximeterTypeMapper;
        this.companyMapper = companyMapper;
    }

        @Override
        public ReturnInfo findTaximeterALL(Integer pageNum, Integer pageSize, TaximeterView taximeterView) {
            if(!pageNum.equals(0)||!pageSize.equals(0)){
                PageHelper.startPage(pageNum, pageSize);
            }
            Integer pagetotal = 0;
            Long total = 0L;
            List<TaximeterView> taximeterViews = taximeterMapper.findTaximeterAll(taximeterView);
            PageInfo<TaximeterView> pageInfo = new PageInfo<>(taximeterViews);
            if(taximeterViews.size()!=0){
                pagetotal = pageInfo.getPages();
                total = pageInfo.getTotal();
            }
           return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
        }

         @Override
         public String updateTaximeter(TaximeterView  taximeterView) {
             if(taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterView.getJjqtype()).get(0).getFactid()!=factMapper.findFactByFactname(taximeterView.getFactname()).get(0).getId()){
                 return "计价器和计价器厂商不匹配,请重新选择";
             }
             //因为是由名称查到id，这里是如果名称不唯一也可以找到一个id
             if(factMapper.findFactByFactname(taximeterView.getFactname())!=null&&factMapper.findFactByFactname(taximeterView.getFactname()).size()>0){
                 Fact fact = factMapper.findFactByFactname(taximeterView.getFactname()).get(0);
                 taximeterView.setFactid(fact.getId());
             }
             if(priceMapper.findPriceByPriceType(taximeterView.getPricetype())!=null&&priceMapper.findPriceByPriceType(taximeterView.getPricetype()).size()>0){
                 Price price = priceMapper.findPriceByPriceType(taximeterView.getPricetype()).get(0);
                 taximeterView.setPriceid(price.getId());
             }
             if(taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterView.getJjqtype())!=null&&taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterView.getJjqtype()).size()>0){
                 TaximeterType taximeterType = taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterView.getJjqtype()).get(0);
                 taximeterView.setJjqtypeid(taximeterType.getId());
             }
             if(taximeterMapper.updateTaximeter(taximeterView)){
                 return "成功";
             }else {
                 return "失败";
             }
         }

         @Override
         public String deleteTaximeter(int id) {
             if(taximeterMapper.deleteTaximeter(id)){
                 return "成功";
             }else {
                 return "失败";
             }
         }

         @Override
         public List<TaximeterView> findTaximeterList(TaximeterView taximeterView) {
                 return taximeterMapper.findTaximeterList(taximeterView);
    }

         @Override
         public String addTaximeter(Taximeter taximeter) {
//             String mes = "成功";
//             if(taximeterView.getFactid()!=null&&taximeterView.getJjqtypeid()!=null&&taximeterView.getPriceid()!=null){
//                 if(taximeterView.getCarInfos()!=null&&taximeterView.getCarInfos().size()!=0){
//                     for(CarInfo carInfo : taximeterView.getCarInfos()){
//                         if(carInfo.getCarno()!=null&&!carInfo.getCarno().equals("")&&carInfo.getCompanyid()!=null){
//                             if(taximeterMapper.findTaximeterByCarno(carInfo.getCarno())!=null){
//                                 mes = "该车牌已存在";
//                                 break;
//                             }
//                             Taximeter taximeter1 = new Taximeter(taximeterView.getFactid(),taximeterView.getJjqtypeid(),taximeterView.getPriceid(),carInfo.getCompanyid(),carInfo.getCarno());
//                             if (!taximeterMapper.addTaximeter(taximeter1)){
//                                 mes = "失败1";
//                                 break;
//                             }
//                         }else {
//                             mes = "失败2";
//                             break;
//                         }
//                     }
//                 }else {
//                     mes = "失败3";
//                 }
//             }else {
//                 mes = "失败4";
//             }
//             return mes;
             if(taximeterTypeMapper.findJjqById(taximeter.getJjqtypeid()).getFactid()!=taximeter.getFactid()){
                 return "计价器和计价器厂商不匹配,请重新选择";
             }
             if(taximeterMapper.findTaximeterByCarno(taximeter.getCarno()).size()!=0){
                 return "车牌号重复";
             }else if(taximeterMapper.addTaximeter(taximeter)){
                 return "成功";
             }else {
                 return "失败";
             }

         }

    @Override
    public String deleteTaximeterall(TaximeterView taximeterView) {
        String mes = "成功";
        if (taximeterView.getTaximeters() != null && taximeterView.getTaximeters().size() != 0) {
            for (Taximeter taximeter : taximeterView.getTaximeters()) {
                if(!taximeterMapper.deleteTaximeter(taximeter.getId())){
                    mes = "失败";
                    break;
                }
            }

        }else {
            mes = "失败";
        }
        return mes;
    }

}
